package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: project-springbootdemo02
 * @BelongsPackage: com.itheima.controller
 * @Author: jiangyi
 * @CreateTime: 2025-08-04  21:07
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("分页查询,参数:{}", studentQueryParam);
        PageResult pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 请求路径后面跟 /value1,value2... 这样的用@PathVariable这个注解接收
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除员工,ids:{}", ids);
        studentService.deleteById(ids);
        return Result.success();
    }

    /**
     * 新增员工
     * @param student
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("保存员工,员工信息:{}", student);
        studentService.save(student);
        return Result.success();
    }
    /**
     * 修改学员(第一步)：主键回显
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询员工信息,id:{}", id);
        Student student = studentService.getInfoById(id);
        return Result.success(student);
    }

    /**
     * 修改学员（第二步）：修改学员信息
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改员工信息,员工信息:{}", student);
        studentService.updateById(student);
        return Result.success();
    }

    @PutMapping ("/violation/{id}/{score}")
    public Result updateViolation(
            @PathVariable Integer id,
            @PathVariable Integer score
    ){
        log.info("根据学员id:{}修改违规分数score:{}", id, score);
        studentService.updateViolationById(id, score);
        return Result.success();
    }

}
